using System.Threading.Tasks;
using UnityEngine.Events;

public static class UnityEventExtensions
{
    /// <summary>
    /// 返回一个 Task，当 event.Invoke() 被调用时该 Task 完成。
    /// </summary>
    public static Task WaitForInvoke(this UnityEvent evt)
    {
        var tcs = new TaskCompletionSource<bool>();
        UnityAction callback = null;
        callback = () =>
        {
            evt.RemoveListener(callback);
            tcs.SetResult(true);
        };
        evt.AddListener(callback);
        return tcs.Task;
    }
}